/*
 * @Author: anxia.ack anchenkai@come-future.com
 * @Date: 2025-03-22 16:04:56
 * @LastEditors: anxia.ack anchenkai@come-future.com
 * @LastEditTime: 2025-04-21 12:39:43
 * @FilePath: /free-learning-front/src/router/index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from "vue-router";
import HomeView from "../views/HomeView.vue";
import { localUtils } from "@/utils/local";
import { ElMessageBox } from "element-plus";
import type { UserInfo } from "@/api/user";

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  scrollBehavior(to, from, savedPosition) {
    return { top: 0 };
  },
  routes: [
    {
      path: "/",
      name: "home",
      component: HomeView,
    },
    {
      path: "/about",
      name: "about",
      // 路由懒加载
      component: () => import("../views/AboutView.vue"),
    },
    // 添加教育平台相关路由
    {
      path: "/courses",
      name: "courses",
      component: () => import("../views/CoursesView.vue"),
    },
    {
      path: "/teachers",
      name: "teachers",
      component: () => import("../views/TeachersView.vue"),
    },
    {
      path: "/consultation",
      name: "consultation",
      component: () => import("../views/ConsultationView.vue"),
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/LoginView.vue"),
    },
    {
      path: "/register",
      name: "register",
      component: () => import("../views/RegisterView.vue"),
    },
    {
      path: "/register-consultant",
      name: "register-consultant",
      component: () => import("../views/RegisteConsultantView.vue"),
    },
    // 动态路由，用于课程详情页
    {
      path: "/courses/:id",
      name: "course-detail",
      component: () => import("../views/CourseDetailView.vue"),
    },
    {
      path: "/profile",
      name: "profile",
      component: () => import("../views/ProfileView.vue"),
    },
    {
      path: "/account/setting",
      name: "account-setting",
      component: () => import("../views/AccountSettingView.vue"),
    },
    {
      path: "/my-course",
      name: "my-course",
      component: () => import("../views/MyCourseView.vue"),
    },
    {
      path: "/course-learning",
      name: "course-learning",
      component: () => import("../views/CourseLearningView.vue"),
    },
    {
      path: "/my-review",
      name: "my-review",
      component: () => import("../views/MyReviewView.vue"),
    },
    {
      path: '/myMessage',
      name: 'my-message',
      component: () => import('@/views/MyMessageView.vue'),
      meta: {
        title: '我的消息',
        requiresAuth: true
      }
    },
    {
      path: "/chat-message",
      name: "chat-message",
      component: () => import("@/views/ChatMessageView.vue"),
    },
  ],
});
const HAVE_TOKEN_ROUTES: string[] = [
  "profile",
  "account-setting",
  "my-course",
  "course-learning",
  "my-message",
  "my-review",
  "chat-message",
];

// 路由守卫 判断是否登录
router.beforeEach((to, from, next) => {
  console.log(to.name);

  if (HAVE_TOKEN_ROUTES.includes(to.name as string)) {
    const userInfo = localUtils.get<UserInfo>("userInfo");
    if (!userInfo?.userId) {
      ElMessageBox.alert("您还未登录，请先登录！", "提示", {
        confirmButtonText: "确定",
        showCancelButton: false,
        type: "warning",
      }).then(() => {
        next({ name: "login", query: { redirect: to.fullPath } });
      });
    } else {
      next();
    }
  } else {
    next();
  }
});
export default router;
